<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PT站点种子加工工具</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f7f7f7;
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .container {
            background-color: #fff;
            border-radius: 5px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .tab-container {
            display: flex;
            margin-bottom: 20px;
        }
        .tab {
            padding: 10px 20px;
            cursor: pointer;
            background-color: #eee;
            border: 1px solid #ddd;
            flex: 1;
            text-align: center;
        }
        .tab.active {
            background-color: #4CAF50;
            color: white;
            border-color: #4CAF50;
        }
        .tab-content {
            display: none;
        }
        .tab-content.active {
            display: block;
        }
        h2 {
            color: #4CAF50;
        }
        form {
            margin-top: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="file"], input[type="text"], input[type="number"] {
            margin-bottom: 15px;
            padding: 8px;
            width: 100%;
            box-sizing: border-box;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
        }
        button:hover {
            background-color: #45a049;
        }
        .section {
            margin-top: 30px;
            border-top: 1px solid #ddd;
            padding-top: 20px;
        }
        .result {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
            background-color: #f9f9f9;
            border: 1px solid #ddd;
            display: none;
        }
        .success {
            border-color: #4CAF50;
            background-color: #e8f5e9;
            color: #2e7d32;
        }
        .error {
            border-color: #f44336;
            background-color: #ffebee;
            color: #c62828;
        }
    </style>
</head>
<body>
    <h1>PT站点种子加工工具</h1>
    
    <div class="tab-container">
        <div class="tab active" onclick="showTab('process')">种子处理</div>
        <div class="tab" onclick="showTab('upload')">上传种子</div>
        <div class="tab" onclick="showTab('download')">下载种子</div>
    </div>
    
    <div id="process" class="tab-content active">
        <div class="container">
            <h2>处理现有种子文件</h2>
            <p>上传您的种子文件并指定要替换的用户ID</p>
            
            <form action="/api/torrent/process" method="post" enctype="multipart/form-data">
                <div>
                    <label for="file">选择种子文件:</label>
                    <input type="file" id="file" name="file" accept=".torrent" required>
                </div>
                
                <div>
                    <label for="userId">用户ID:</label>
                    <input type="number" id="userId" name="userId" placeholder="例如: 104" required>
                </div>
                
                <button type="submit">处理文件</button>
            </form>
        </div>
        
        <div class="container section">
            <h2>处理本地文件</h2>
            <p>指定本地文件路径处理种子</p>
            
            <form id="localForm" onsubmit="processLocalFile(event)">
                <div>
                    <label for="filePath">本地文件路径:</label>
                    <input type="text" id="filePath" name="filePath" placeholder="例如: D:/torrents/example.torrent" required>
                </div>
                
                <div>
                    <label for="localUserId">用户ID:</label>
                    <input type="number" id="localUserId" name="userId" placeholder="例如: 104" required>
                </div>
                
                <button type="submit">处理文件</button>
            </form>
        </div>
    </div>
    
    <div id="upload" class="tab-content">
        <div class="container">
            <h2>上传种子文件</h2>
            <p>上传普通种子文件，系统将自动设置tracker并添加您的用户ID作为passkey</p>
            
            <form id="uploadForm" onsubmit="uploadTorrent(event)">
                <div>
                    <label for="uploadFile">选择种子文件:</label>
                    <input type="file" id="uploadFile" name="file" accept=".torrent" required>
                </div>
                
                <div>
                    <label for="uploadUserId">上传者用户ID:</label>
                    <input type="number" id="uploadUserId" name="userId" placeholder="例如: 103" required>
                </div>
                
                <button type="submit">上传种子</button>
            </form>
            
            <div id="uploadResult" class="result"></div>
        </div>
    </div>
    
    <div id="download" class="tab-content">
        <div class="container">
            <h2>下载种子文件</h2>
            <p>下载种子文件，系统将自动替换passkey为您的用户ID</p>
            
            <form id="downloadForm" onsubmit="downloadTorrent(event)">
                <div>
                    <label for="filePath">种子文件路径:</label>
                    <input type="text" id="downloadPath" name="path" placeholder="例如: D:/PT/files/example.torrent" required>
                </div>
                
                <div>
                    <label for="downloadUserId">下载者用户ID:</label>
                    <input type="number" id="downloadUserId" name="userId" placeholder="例如: 104" required>
                </div>
                
                <button type="submit">下载种子</button>
            </form>
            
            <div id="downloadResult" class="result"></div>
        </div>
    </div>

    <script>
        // 切换标签页
        function showTab(tabId) {
            // 隐藏所有标签内容
            document.querySelectorAll('.tab-content').forEach(content => {
                content.classList.remove('active');
            });
            
            // 移除所有标签的active类
            document.querySelectorAll('.tab').forEach(tab => {
                tab.classList.remove('active');
            });
            
            // 显示选中的标签内容
            document.getElementById(tabId).classList.add('active');
            
            // 激活对应的标签按钮
            const tabs = document.querySelectorAll('.tab');
            for (let i = 0; i < tabs.length; i++) {
                if (tabs[i].textContent.toLowerCase().includes(tabId)) {
                    tabs[i].classList.add('active');
                    break;
                }
            }
        }
        
        // 处理本地文件
        function processLocalFile(event) {
            event.preventDefault();
            
            const filePath = document.getElementById('filePath').value;
            const userId = document.getElementById('localUserId').value;
            
            if (!filePath || !userId) {
                alert('请填写所有必要信息!');
                return;
            }
            
            // 重定向到处理接口
            window.location.href = `/api/torrent/process-local?filePath=${encodeURIComponent(filePath)}&userId=${userId}`;
        }
        
        // 上传种子文件
        function uploadTorrent(event) {
            event.preventDefault();
            
            const fileInput = document.getElementById('uploadFile');
            const userId = document.getElementById('uploadUserId').value;
            const resultDiv = document.getElementById('uploadResult');
            
            if (!fileInput.files.length || !userId) {
                resultDiv.textContent = '请选择文件并填写用户ID!';
                resultDiv.className = 'result error';
                resultDiv.style.display = 'block';
                return;
            }
            
            const formData = new FormData();
            formData.append('file', fileInput.files[0]);
            formData.append('userId', userId);
            
            resultDiv.textContent = '正在上传和处理文件...';
            resultDiv.className = 'result';
            resultDiv.style.display = 'block';
            
            fetch('/api/torrent/upload', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(result => {
                if (result.code === 0) {
                    // 成功
                    resultDiv.textContent = result.message || '种子文件已成功上传并处理!';
                    resultDiv.className = 'result success';
                    
                    // 显示下载链接
                    if (result.data && result.data.downloadUrl) {
                        const downloadLink = document.createElement('a');
                        downloadLink.href = result.data.downloadUrl;
                        downloadLink.textContent = '点击下载处理后的种子文件';
                        downloadLink.style.display = 'block';
                        downloadLink.style.marginTop = '10px';
                        resultDiv.appendChild(downloadLink);
                    }
                } else {
                    // 失败
                    resultDiv.textContent = result.message || '上传失败: 未知错误';
                    resultDiv.className = 'result error';
                }
            })
            .catch(error => {
                resultDiv.textContent = `上传失败: ${error.message}`;
                resultDiv.className = 'result error';
            });
        }
        
        // 下载种子文件
        function downloadTorrent(event) {
            event.preventDefault();
            
            const filePath = document.getElementById('downloadPath').value;
            const userId = document.getElementById('downloadUserId').value;
            const resultDiv = document.getElementById('downloadResult');
            
            if (!filePath || !userId) {
                resultDiv.textContent = '请填写文件路径和用户ID!';
                resultDiv.className = 'result error';
                resultDiv.style.display = 'block';
                return;
            }
            
            resultDiv.textContent = '正在处理和下载文件...';
            resultDiv.className = 'result';
            resultDiv.style.display = 'block';
            
            // 构建请求URL
            const infoUrl = `/api/torrent/info?path=${encodeURIComponent(filePath)}`;
            const downloadUrl = `/api/torrent/download?path=${encodeURIComponent(filePath)}&userId=${userId}`;
            
            // 先检查文件是否存在
            fetch(infoUrl)
                .then(response => response.json())
                .then(result => {
                    if (result.code === 0) {
                        // 文件存在，触发下载
                        window.location.href = downloadUrl;
                        resultDiv.textContent = '文件处理成功，下载已开始!';
                        resultDiv.className = 'result success';
                        
                        // 显示文件信息
                        if (result.data) {
                            const infoDiv = document.createElement('div');
                            infoDiv.innerHTML = `
                                <p>文件名: ${result.data.fileName}</p>
                                <p>文件大小: ${formatFileSize(result.data.fileSize)}</p>
                            `;
                            resultDiv.appendChild(infoDiv);
                        }
                    } else {
                        // 文件不存在
                        resultDiv.textContent = result.message || '文件检查失败';
                        resultDiv.className = 'result error';
                    }
                })
                .catch(error => {
                    // 发生错误，直接尝试下载
                    window.location.href = downloadUrl;
                    resultDiv.textContent = '文件处理请求已发送，如果文件存在将自动下载!';
                    resultDiv.className = 'result success';
                });
        }
        
        // 格式化文件大小
        function formatFileSize(bytes) {
            if (bytes === 0) return '0 B';
            const k = 1024;
            const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }
    </script>
</body>
</html> 